﻿using Corina.Commands;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class IndustryViewmodel : ViewmodelBase
    {
        public IndustryViewmodel()
        {
            CommonBaseClient.GetIndustryAllItemsCompleted += GetIndustryAllItemsCompleted;
            CommonBaseClient.InsertIndustryCompleted += InsertIndustryCompleted;
            CommonBaseClient.UpdateIndustryCompleted += UpdateIndustryCompleted;
            CommonBaseClient.ChangeIndustryAvavailabilityCompleted += ChangeIndustryAvavailabilityCompleted;
            CommonBaseClient.RemoveIndustryCompleted += RemoveIndustryCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        #region methods
        private void RemoveIndustryCompleted(object sender, DataService.RemoveIndustryCompletedEventArgs e)
        {
            LoadData();
        }

        private void ChangeIndustryAvavailabilityCompleted(object sender, DataService.ChangeIndustryAvavailabilityCompletedEventArgs e)
        {
            LoadData();
        }

        private void UpdateIndustryCompleted(object sender, DataService.UpdateIndustryCompletedEventArgs e)
        {
            Operation = EntityOperation.None;
            UpdateEntity = new IndustryInfo();
            LoadData();
        }

        private void InsertIndustryCompleted(object sender, DataService.InsertIndustryCompletedEventArgs e)
        {
            Operation = EntityOperation.None;
            UpdateEntity = new IndustryInfo();
            LoadData();
        }

        private void GetIndustryAllItemsCompleted(object sender, DataService.GetIndustryAllItemsCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                if(e.Result.Result == Enumerations.ResultValue.Succeed)
                {
                    ItemsCollection = new ObservableCollection<IndustryInfo>(e.Result.Entities.OfType<IndustryInfo>());
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        protected override void LoadData(params object[] paras)
        {
            CommonBaseClient.GetIndustryAllItemsAsync(Account, Password);
        }

        #endregion

        #region Properties
        private string _keywords = string.Empty;
        public string Keywords
        {
            get { return _keywords; }
            set
            {
                _keywords = value;
                RaisePropertyChanged(() => Keywords);
                RaisePropertyChanged(() => DisplayCollection);
            }
        }

        private IndustryInfo _updateEntity = new IndustryInfo();
        public IndustryInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }

        private ObservableCollection<IndustryInfo> _itemsCollection;
        public ObservableCollection<IndustryInfo> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
                RaisePropertyChanged(() => DisplayCollection);
            }
        }

        public IEnumerable<IndustryInfo> DisplayCollection
        {
            get
            {
                return ItemsCollection == null ? null : ItemsCollection.Where(t => t.Title.Contains(Keywords) || t.Description.Contains(Keywords));
            }
        }
        #endregion

        #region Commands
        public ICommand InsertCommand
        {
            get {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        UpdateEntity = new IndustryInfo();
                        Operation = EntityOperation.Insert;
                    }
                };
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (Operation == EntityOperation.Update)
                        {
                            CommonBaseClient.UpdateIndustryAsync(Account,
                                Password,
                                UpdateEntity.Title,
                                UpdateEntity.Description,
                                UpdateEntity.IsAvailable,
                                UpdateEntity.Id);
                        }
                        else
                        {
                            CommonBaseClient.InsertIndustryAsync(Account,
                                Password,
                                UpdateEntity.Title,
                                UpdateEntity.Description,
                                UpdateEntity.IsAvailable);
                        }
                    }
                };
            }
        }

        public ICommand EditCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    UpdateEntity = ItemsCollection.FirstOrDefault(i => i.Id == id);
                    Operation = EntityOperation.Update;
                });
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    if (MessageBox.Show("确定要删除该条数据吗？", "删除确认", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        int id = Convert.ToInt32(t);
                        CommonBaseClient.RemoveIndustryAsync(Account, Password, id);
                    }
                });
            }
        }

        public ICommand ChangeCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    bool flag = !ItemsCollection.FirstOrDefault(i => i.Id == id).IsAvailable;
                    CommonBaseClient.ChangeIndustryAvavailabilityAsync(Account, Password, id, flag);
                });
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        UpdateEntity = new IndustryInfo();
                        Operation = EntityOperation.None;
                    }
                };
            }
        }
        #endregion
    }
}
